package com.zsj.comminlibrary.util.log;

import android.content.Context;
import android.os.Process;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.text.SimpleDateFormat;

public final class XLog {

    private static Logger sLogger;
    private static String sClientId = "";
    private static final int PID = Process.myPid();
    private static SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private static boolean sEnableUpload;
    private static boolean hasInit;


    public static void init(Context context, boolean debug, boolean writeFile, String logDir) {
        try {
            hasInit = true;

            FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                    .showThreadInfo(true)  // (Optional) Whether to show thread info or not. Default true
                    .methodCount(2)         // (Optional) How many method line to show. Default 2
                    .methodOffset(7)        // (Optional) Hides internal method calls up to offset. Default 5
                    .tag("base")   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                    .build();

            Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
            Logger.addLogAdapter(new MyDiskLogAdapter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void d(String msg) {
        if (hasInit)
            try {
                Logger.d(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void i(String msg) {
        if (hasInit)
            try {
                Logger.i(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void f(String msg) {
        if (hasInit) {
            try {
                Logger.e(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void w(String msg) {
        if (hasInit)
            try {
                Logger.w(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void e(String msg) {
        if (hasInit)
            try {
                Logger.e(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void e(Throwable exception) {
        if (hasInit)
            try {
                Logger.e("", exception);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void setClientId(String clientId) {
        if (hasInit)
            try {
                XLog.sClientId = clientId;
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void setEnableUpload(boolean sEnableUpload) {
        if (hasInit)
            try {
                XLog.sEnableUpload = sEnableUpload;
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
